package store.lunangangster.nodeflow.parser.el.operator;

import static store.lunangangster.nodeflow.enums.FlowType.FLOW_WHEN;

import com.ql.util.express.Operator;
import java.util.ArrayList;
import java.util.List;
import store.lunangangster.nodeflow.builder.FlowBuilder;
import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.rt.FlowBus;
import store.lunangangster.nodeflow.rt.element.Executable;
import store.lunangangster.nodeflow.rt.element.flow.WhenFlow;

/**
 * WHEN表达式所关联的操作符
 *
 * @author cwk
 * @version 1.0
 */
public class WhenOperator extends Operator {

  @Override
  public WhenFlow executeInner(Object[] list) throws RuleSourceParseException {
    List<Executable> executableList = new ArrayList<>();
    for (Object o : list) {
      if (o instanceof Executable) {
        executableList.add((Executable) o);
        continue;
      }
      String id = (String) o;
      Executable executable = FlowBus.getExecutable(id);
      if (executable == null) {
        throw new RuleSourceParseException(
            String.format("Find no available CommonNode or Chain for expression: %s", o));
      }
      executableList.add(executable);
    }
    return (WhenFlow) FlowBuilder.buildELFLow(FLOW_WHEN, executableList);
  }

}
